package com.whfc.fuum.redis.impl;

import com.whfc.fuum.redis.SysExternalLinkRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-08-03 21:04
 */
@Repository
public class SysExternalLinkRedisDaoImpl implements SysExternalLinkRedisDao {

    private static final String EXTERNAL_LINK_URL = "qylb_link_url::%s";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setQylbUrl(Integer userId, String linkUrl) {
        if (isDefaultUser(userId)) {
            String key = String.format(EXTERNAL_LINK_URL, userId);
            redisTemplate.opsForValue().set(key, linkUrl, 5, TimeUnit.MINUTES);
        }
    }

    @Override
    public String getQylbUrl(Integer userId) {
        if (isDefaultUser(userId)) {
            String key = String.format(EXTERNAL_LINK_URL, userId);
            return redisTemplate.opsForValue().get(key);
        }
        return null;
    }

    /**
     * 是否默认用户
     *
     * @param userId
     * @return
     */
    private boolean isDefaultUser(Integer userId) {
        return userId != null && userId == 0;
    }
}
